﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Microsoft.AspNetCore.Mvc;
using ISerives;
using Model;
using Newtonsoft.Json;

namespace HR.Controllers
{
    public class salaryCriterionController : Controller
    {
        private readonly IEsalary_standard_Servies iess;
        private readonly Iconfig_public_charSerives icp;
        private readonly Isalary_standard_details_Serives ids;

        public salaryCriterionController(IEsalary_standard_Servies iess,Iconfig_public_charSerives icp,Isalary_standard_details_Serives ids)
        {
            this.iess = iess;
            this.icp = icp;
            this.ids = ids;
        }
        public IActionResult Index()
        {
            return View();
        }

        #region 薪酬登记
        /// <summary>
        /// 薪酬添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> salarystandard()
        {
           List<config_public_char> list=await icp.SelectShow("薪酬设置");
            ViewBag.s = list;
         
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> salarystandard(salary_standard ess)
        {
            Esalary_standard ess1 = new Esalary_standard()
            {
                regist_time = DateTime.Now,
                standard_id=ess.standard_id,
                standard_name=ess.standard_name,
                designer=ess.designer,
                register=ess.register,
                salary_sum=ess.salary_sum,
                remark=ess.remark
            };
            if (ModelState.IsValid)
            {
                int i = await iess.Add(ess1);
                if (i > 0)
                {
                    return Content(JsonConvert.SerializeObject("T"));

                }
                else
                {
                    return Content("F");
                }
            }
            else
            {
                return View();
            }

        }
        public async Task<IActionResult> register2(Esalary_standard_details s)
        {

            if (ModelState.IsValid)
            {
                int num = await ids.Add(s);
                return Content(JsonConvert.SerializeObject("T2")); ;
            }
            else
            {
                return View();
            }
        }
        #endregion

        #region 薪酬查询分页
        public async  Task<IActionResult> salarystandard_change_list(int currentpage)
        {
            FenYe fenye = new FenYe()
            {
                Where= "check_status=0",
                CurrentPage= currentpage,
                PageSize=2
            };
            List<Esalary_standard> list =await iess.FenYe(fenye);
            if (fenye.CurrentPage==0)
            {
                fenye.CurrentPage = 1;
            }
            ViewData["cp"] = fenye.CurrentPage;
            ViewData["c"] = fenye.CurrentPage;
            ViewBag.rows = fenye.Rows;
            ViewBag.pages = fenye.Pages;
            ViewBag.pageSize = fenye.PageSize;
            ViewData.Model =list;
            return View();
        }
        #endregion

        #region 薪酬复核
        /// <summary>
        /// 薪酬复核
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> salarystandard_change(int id,string sid)
        {
           Esalary_standard es= await iess.ShowId(id);
           List<salary_standard_details> list= await ids.SelectId(sid);
            ViewBag.s = list;
            return View(es);
        }
        [HttpPost]
        public async Task<IActionResult> salarystandard_change(Esalary_standard ess)
        {
            Esalary_standard es = new Esalary_standard()
            {
            ssd_id=ess.ssd_id,
            standard_id = ess.standard_id,// : 薪酬标准单编号
            standard_name = ess.standard_name,// : 薪酬标准单名称
            designer = ess.designer,// : 制定者名字
            checker = ess.checker,//: 复核人
            check_time = DateTime.Now, //: 复核时间
            salary_sum = ess.salary_sum,//: 薪酬总额
            check_status =1,// : 是否经过复核
            check_comment = ess.check_comment//: 复核意见
            };
           
            if (await iess.FH_Upd(es)>0)
            {
                return RedirectToAction("salarystandard_change_list");
            }
            else
            {
                return View(ess);
            }
           
           
        }
        #endregion

        #region 薪酬标准查询
        public IActionResult salarystandard_query_locate()
        {
            return View();
        }
        public async Task<IActionResult> salarystandard_query_list(int currentpage,Esalary_standard ss)
        {
            FenYe fenye = new FenYe()
            {
                /* AND regist_time>='{ss.regist_time}' AND regist_time<='{ss.check_time}'*/
                Where = $"check_status=1 AND standard_id like '%{ss.standard_id}%' AND standard_name like '%{ss.standard_name}%'",
                CurrentPage = currentpage,
                PageSize = 2
            };
            List<Esalary_standard> list = await iess.FenYe(fenye);
            if (fenye.CurrentPage == 0)
            {
                fenye.CurrentPage = 1;
            }
            ViewData["cp"] = fenye.CurrentPage;
            ViewData["c"] = fenye.CurrentPage;
            ViewBag.rows = fenye.Rows;
            ViewBag.pages = fenye.Pages;
            ViewBag.pageSize = fenye.PageSize;
            ViewData.Model = list;
            return View();
        }

        public async Task<IActionResult> salarystandard_query(string id)
        {
          salary_standard list=await iess.ShowId_sa(id);
            ViewData.Model = list;
            List<salary_standard_details> list2 = await ids.SelectId(id);
            ViewBag.s = list2;
            return View();
        }
        #endregion

        #region 薪酬更改
        public IActionResult salarystandard_change_locate()
        {
           return View();
        }
       
        public async Task<IActionResult> salarystandard_check_list(int currentpage, Esalary_standard ss)
        {
            FenYe fenye = new FenYe()
            {

                Where = $"check_status=0 AND standard_id like '%{ss.standard_id}%' AND standard_name like '%{ss.standard_name}%'",
                CurrentPage = currentpage,
                PageSize = 2
            };
            List<Esalary_standard> list = await iess.FenYe(fenye);
            if (fenye.CurrentPage == 0)
            {
                fenye.CurrentPage = 1;
            }
            ViewData["cp"] = fenye.CurrentPage;
            ViewData["c"] = fenye.CurrentPage;
            ViewBag.rows = fenye.Rows;
            ViewBag.pages = fenye.Pages;
            ViewBag.pageSize = fenye.PageSize;
            ViewData.Model = list;
            return View();
        }

        /// <summary>
        /// 这个更改和核实名字替换了
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> salarystandard_check(int id)
        {
            Esalary_standard es = await iess.ShowId(id);
            List<config_public_char> list = await icp.SelectShow("薪酬设置");
            List<Esalary_standard_details> Elist = new List<Esalary_standard_details>();
            for (int i = 0; i < 6; i++)
            {
                Esalary_standard_details d = new Esalary_standard_details()
                {
                    item_id = list[i].pbc_id,
                    item_name = list[i].attribute_name
                };
                Esalary_standard_details d2 = await ids.Select(es.standard_id, list[i].pbc_id);
                if (d2 != null)
                {
                    d.sdt_id = d2.sdt_id;
                    d.salary = d2.salary;
                }
                else
                {
                    d.salary = 0.00; 
                }
                Elist.Add(d);
            }
            ViewBag.s = Elist;
            return View(es);
        }
        [HttpPost]
        public async Task<IActionResult> salarystandard_checkUpd(Esalary_standard ess)
        {
            Esalary_standard ess1 = new Esalary_standard()
            {
                change_status=1,
                ssd_id=ess.ssd_id,
                change_time = DateTime.Now,
                standard_id = ess.standard_id,
                standard_name = ess.standard_name,
                designer = ess.designer,
                changer = ess.changer,
                salary_sum = ess.salary_sum,
                remark = ess.remark
            };
            Esalary_standard_details es = new Esalary_standard_details()
            {
                standard_id = ess1.standard_id
            };
            List<salary_standard_details> list = await ids.SelectId(ess1.standard_id);
                int i = await iess.GB_Upd(ess1);
                if (i > 0)
                {
                if (list.Count!=0)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        await ids.Del(es);
                    }
                }
                return Content(JsonConvert.SerializeObject("T"));
                }
                else
                {
                    return Content("F");
                }
        }
        public async Task<IActionResult> Upd2(Esalary_standard_details s)
        {
                int num = await ids.Add(s);
                return Content(JsonConvert.SerializeObject("T2"));
        }
        #endregion



        } 
}